<?php

/**
 * This is the model class for table "match".
 *
 * The followings are the available columns in table 'match':
 * @property string $ID
 * @property integer $YEAR
 * @property string $ROUND
 * @property string $NAME
 * @property string $VOTE
 * @property string $O_NAME
 * @property string $O_VOTE
 * @property string $TYPE
 * @property string $T_VOTE
 * @property string $D_VOTE
 * @property double $P_VOTE
 */
class Match extends CActiveRecord
{

	public $TIMES;
	public $SUM_VOTES;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Match the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'match';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('ID, YEAR, ROUND, NAME, VOTE, O_NAME, O_VOTE, TYPE, T_VOTE, D_VOTE, P_VOTE', 'required'),
				array('YEAR', 'numerical', 'integerOnly'=>true),
				array('P_VOTE', 'numerical'),
				array('ID, TYPE', 'length', 'max'=>250),
				array('ROUND, VOTE, O_VOTE, T_VOTE, D_VOTE', 'length', 'max'=>10),
				array('NAME, O_NAME', 'length', 'max'=>1024),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array('ID, YEAR, ROUND, NAME, VOTE, O_NAME, O_VOTE, TYPE, T_VOTE, D_VOTE, P_VOTE', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'ID' => 'ID',
				'YEAR' => Yii::t('isml_match','year'),
				'ROUND' => Yii::t('isml_match','round'),
				'NAME' => Yii::t('isml_match','name'),
				'VOTE' => Yii::t('isml_match','vote'),
				'O_NAME' => Yii::t('isml_match','oname'),
				'O_VOTE' => Yii::t('isml_match','ovote'),
				'TYPE' => Yii::t('isml_match','type'),
				'T_VOTE' => Yii::t('isml_match','tvote'),
				'D_VOTE' => Yii::t('isml_match','dname'),
				'P_VOTE' => Yii::t('isml_match','pname'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID);
		$criteria->compare('YEAR',$this->YEAR);
		if (strpos($this->ROUND,'-') !== false) {
			$roundArray=explode("-",$this->ROUND);
			$criteria->addBetweenCondition("ROUND", $roundArray[0],$roundArray[1]);
		}
		else
		{
			$criteria->compare('ROUND',$this->ROUND);
		}
		$criteria->compare('NAME',$this->NAME);
		$criteria->compare('VOTE',$this->VOTE);
		$criteria->compare('O_NAME',$this->O_NAME);
		$criteria->compare('O_VOTE',$this->O_VOTE);
		$criteria->compare('TYPE',$this->TYPE);
		$criteria->compare('T_VOTE',$this->T_VOTE);
		$criteria->compare('D_VOTE',$this->D_VOTE);
		$criteria->compare('P_VOTE',$this->P_VOTE);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'sort'=>array(
						'defaultOrder'=>'YEAR DESC,ROUND ASC,VOTE DESC',
				),
				'pagination'=>array(
						'pageSize'=>100
				),
		));
	}


	public function getWinTimes()
	{

		$criteria= new CDbCriteria();
		$rawData=Yii::app()->db->createCommand('SELECT DISTINCT(t.NAME),IFNULL(V1.SUM_NUM, 0) AS `TIMES` '.
				'FROM `match` `t` LEFT JOIN '.
				'(SELECT M1.ID,M1.NAME NAME1,COUNT(1) SUM_NUM FROM `match` M1 '.
				'where M1.D_VOTE>=0 GROUP BY NAME1) AS V1 ON (V1.NAME1=t.NAME) '
				.'ORDER BY TIMES')->queryAll();


// 		$criteria=array(
// 				'select'=>'DISTINCT(t.NAME),IFNULL(V1.SUM_NUM, 0) AS `TIMES`,',
// 				'join'=>' LEFT JOIN (SELECT M1.ID,M1.NAME NAME1,COUNT(1) SUM_NUM '.
// 				' FROM `MATCH` M1 where M1.D_VOTE>=0 GROUP BY NAME1) AS V1'
// 				.' ON (V1.NAME1=t.NAME AND V1.ID=t.ID)',
// 				'order'=>'TIMES DESC'
// 		);

		
		return $dataProvider=new CArrayDataProvider($rawData, array(
				'keyField'=>'NAME',
				'sort'=>array(
						'defaultOrder'=>'TIMES DESC',
				),
				'pagination'=>array(
						'pageSize'=>100,
				),
		));
		
// 		return new CActiveDataProvider($this, array(
// 				'criteria'=>$criteria,

// 				'pagination'=>array(
// 						'pageSize'=>100
// 				),
// 		));
	}



	public function getVotes()
	{

		$criteria= new CDbCriteria();
		$criteria=array(
				'select'=>'NAME,SUM(VOTE) as SUM_VOTES',
				'group'=>'NAME',
				'order'=>'SUM_VOTES DESC'
		);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array(
						'pageSize'=>100
				),
		));
	}

	public function getNameLocalized()
	{
		return Yii::t("isml_name", $this->NAME);
	}


	public function getColorForVote($vote)
	{
		// 		if($vote>2000)
		// 		{
		// 			return "blue";
			// 		}
			// 		else if($vote>1000)
			// 		{
			// 			return "green";
				// 		}
				// 		else if($vote>0)
				// 		{
				// 			return "black";
					// 		}
					// 		else if($vote>-1000)
					// 		{
					// 			return "#FF0000";
						// 		}
						// 		else if($vote>-2000)
						// 		{
						// 			return "yellow";
							// 		}
							// 		else
							// 		{
							// 			return "red";
							// 		}
							return $vote;

						}


					}